﻿using System.Collections;
using System.Windows;
using System.Windows.Controls;

namespace TextFileReaderModule.Views
{
	/// <summary>
	/// Interaction logic for TextFileContentView.xaml
	/// </summary>
	public partial class TextFileContentView : UserControl
  {

    #region constructor(s)

    /// <summary>
    /// Creates a new <see cref="TextFileContentView"/> instance.
    /// </summary>
    public TextFileContentView()
		{
			InitializeComponent();
			TextFileContentViewListView.SelectionChanged += TextFileContentViewListViewOnSelectionChanged;
		}

    #endregion

    #region methods

    /// <summary>
    /// Listens on the Selectionchanged event of <see cref="TextFileContentViewListView"/>
    /// </summary>
    /// <param name="sender">Not used.</param>
    /// <param name="selectionChangedEventArgs">Used to get the new selected item value.</param>
    private void TextFileContentViewListViewOnSelectionChanged(
      object sender, SelectionChangedEventArgs selectionChangedEventArgs)
    {
      object newSelectedItem = null;
      if (selectionChangedEventArgs.AddedItems.Count > 0)
        newSelectedItem = selectionChangedEventArgs.AddedItems[0];

      SelectedItem = newSelectedItem;
    }

    #endregion

    #region ItemsSource dependency property

    /// <summary>
		/// Represents <see cref="ItemsSource"/>
		/// </summary>
		public readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register(
		  "ItemsSource", typeof( IList ), typeof( TextFileContentView ), new PropertyMetadata( default( IList ) ) );

		/// <summary>
		/// Gets or sets the items source.
		/// Needed to be used from the "calling" control.
		/// </summary>
		public IList ItemsSource
		{
			get
			{
				return (IList)GetValue( ItemsSourceProperty );
			}
			set
			{
				SetValue( ItemsSourceProperty, value );
			}
		}

		#endregion

		#region ScrollSelectedItem dependency property

		/// <summary>
		/// Determines whether the selected ListView item is bought into view when enabled
		/// </summary>
		public static readonly DependencyProperty ScrollSelectedItemIntoViewProperty =
			DependencyProperty.Register(
				"ScrollSelectedItemIntoView",
				typeof( bool ),
				typeof( TextFileContentView ),
				new FrameworkPropertyMetadata( 
					false, // default value
					OnScrollSelectedItemIntoViewChanged
				) );

		/// <summary>
		/// Represents <see cref="ScrollSelectedItemIntoViewProperty"/>
		/// </summary>
		public bool ScrollSelectedItemIntoView 
		{
			get
			{
				return (bool)GetValue(ScrollSelectedItemIntoViewProperty);
			}
			set
			{
				SetValue( ScrollSelectedItemIntoViewProperty , value);
			}
		}

		/// <summary>
		/// Dependency property <see cref="ScrollSelectedItemIntoViewProperty"/> did change.
		/// Scrolls the view such way that the selected item will be visible for the user.
		/// </summary>
		/// <param name="depObj">Should be a ListView object.</param>
		/// <param name="e">Used to determine the current and the previous value.</param>
		public static void OnScrollSelectedItemIntoViewChanged(
			DependencyObject depObj, DependencyPropertyChangedEventArgs e )
		{
			if( !(e.NewValue is bool) ) return;
			if (!((bool)e.NewValue)) return;	// only interested in true

			var textFileContentView = depObj as TextFileContentView;
			if (textFileContentView == null) return;

			ListView listView = textFileContentView.TextFileContentViewListView;
			if (listView == null) return;

			object selectedListViewItem = textFileContentView.SelectedItem;
			if( selectedListViewItem == null ) return;

			listView.SelectedItem = selectedListViewItem;
			listView.ScrollIntoView( listView.SelectedItem );
		}

		#endregion

		#region SelectedItem dependency property

		/// <summary>
		/// Gets or sets the selected item
		/// </summary>
		public static readonly DependencyProperty SelectedItemProperty =
			DependencyProperty.Register(
				"SelectedItem",
				typeof( object ),
				typeof( TextFileContentView ),
				new FrameworkPropertyMetadata(
					null, // default value
					OnSelectedItemChanged
				) );

		/// <summary>
		/// Dependency property <see cref="SelectedItemProperty"/> did change.
		/// ... will not be used.
		/// </summary>
		/// <param name="depObj">Should be a object.</param>
		/// <param name="e">Used to determine the current and the previous value.</param>
		public static void OnSelectedItemChanged(
			DependencyObject depObj, DependencyPropertyChangedEventArgs e )
		{
			var textFileContentView = depObj as TextFileContentView;
			if( textFileContentView == null ) return;

			ListView listView = textFileContentView.TextFileContentViewListView;
			if( listView == null ) return;

			listView.SelectedItem = e.NewValue;
      textFileContentView.SelectedItem = listView.SelectedItem;
		}

		/// <summary>
		/// Represents <see cref="SelectedItemProperty"/>
		/// </summary>
		public object SelectedItem
		{
			get
			{
				return GetValue( SelectedItemProperty );
			}
			set
			{
				SetValue( SelectedItemProperty, value );
			}
		}

		#endregion
	}
}
